package com.daoxiang.service.impl;

import com.daoxiang.dao.UserMapper;
import com.daoxiang.pojo.User;
import com.daoxiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int saveUser(User user) {
        return this.userMapper.insert(user);
    }

    @Override
    public int deleteUser(int deleteUid) {
        return this.userMapper.deleteByPrimaryKey(deleteUid);
    }

    @Override
    public int updateUser(User user) {
        return this.userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<User> getUserByAccountAndPassword(String username, String password) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (username.length() == 11) {
            criteria.andEqualTo("tel", username).andEqualTo("password", password);
        } else {
            criteria.andEqualTo("account", username).andEqualTo("password", password);
        }
        return this.userMapper.selectByExample(example);
    }

    @Override
    public List<User> getUserByAccount(String username) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("account", username);
        return this.userMapper.selectByExample(example);
    }

    @Override
    public int updateUserUidAndDefaultAddress(User user) {
        return this.userMapper.updateByPrimaryKeySelective(user);
    }

}
